################################################################################
#
#                 Bringing Home the Bacon (World Politics)
#
#                               Kim & Fu  
#
#                              August 2024
#
################################################################################

library(tidyverse)
library(ggthemes)
library(lubridate)
library(stringr)
library(lfe)
library(stargazer)

################# PART 3: SUPPLEMENTARY ANALYSES IN APPENDIX  #################

# set working directory
setwd("~/")

load("export_ambassador.RData")

# Table A.1: Background of Ambassadors, 2002–2020
table(ambassador_raw$country, ambassador_raw$type, exclude = T)



# Figure A.1: Appointment of Ambassadors by the Size of Export Markets
ambassador <- read.csv("ambassador_bush-trump.csv",na.strings = c("", NA))
export_ave <- read.csv("refined_us_export_all_countries_2002_2020.csv")

log_export_ave <- export_ave %>%
  mutate(log_export_ave = log(av_export_usd_2002_2020)) %>%
  mutate(Country2 = case_when(Country=="The Bahamas" ~ "Bahamas",
                              Country=="Myanmar" ~ "Burma",
                              Country=="Democratic Republic of the Congo" ~ "Congo (DR)",
                              Country=="Republic of Congo" ~ "Congo (Republic of)",
                              Country=="Eswatini" ~ "Swaziland",
                              Country=="Fiji" ~ "Fiji, Kiribati, Nauru, Tonga, & Tuvalu",
                              Country=="France" ~ "France & Monaco",
                              Country=="Italy" ~ "Italy & San Marino",
                              Country=="Lao P.D.R." ~ "Laos",
                              Country=="Madagascar" ~ "Madagascar & Comoros",
                              Country=="Mauritius" ~ "Mauritius & Seychelles",
                              Country=="New Zealand" ~ "New Zealand & Samoa",
                              Country=="Papua New Guinea" ~ "Papua New Guinea, Solomon Islands & Vanuatu",
                              Country=="Senegal" ~ "Senegal & Guinea-Bissau",
                              Country=="Slovak Republic" ~ "Slovakia",
                              Country=="Switzerland" ~ "Switzerland & Liechtenstein",
                              Country=="Spain" ~ "Spain & Andorra",
                              Country=="Sri Lanka" ~ "Sri Lanka & Maldives",
                              Country=="North Macedonia" ~ "Macedonia",
                              TRUE ~ Country))

amb_export_ave <- ambassador %>%
  left_join(log_export_ave, by=c("Country" = "Country2")) %>%
  select(-Country.y) %>%
  mutate(Type_Tertiary = ifelse(Home_State_Politician==1, "Home_State", Type_Binary)) %>%
  mutate(Type_Tertiary2 = ifelse(is.na(Type_Tertiary), "Career", Type_Tertiary)) %>%
  mutate(Type_Tertiary3 = case_when(Type_Tertiary2=="Political" ~ "Non-politician",
                                    Type_Tertiary2=="Career" ~ "Diplomat",
                                    Type_Tertiary2=="Home_State" ~ "Politician")) %>%
  left_join(exp30, by = c("Country"="country")) %>%
  mutate(top30_y = ifelse(is.na(top30_y), 0, top30_y))

# (6*8 inch, landscape)
amb_export_ave %>%
  ggplot(aes(x=log_export_ave, fill=Type_Tertiary3)) +
  geom_histogram(alpha=0.7, position="identity", binwidth=0.2, color = "black") +
  theme_few() +
  theme(legend.position = "bottom") +
  scale_fill_manual(values=c("gray80", "gray50", "black")) +
  labs(x="Average US Export Value to a Country in Log (2002-2020)", y="Count of ambassadors",
       fill = "Ambassador Type",
       title = "Distribution of ambassadors (N=1,051), 2002-2020")



# Figure A.2: Appointment of Ambassadors by the Number of Tourists
tourist <- read.csv("international-tourist-trips.csv")

tourist <- tourist %>%
  filter(Year==2002) %>%
  select(Entity, Inbound.arrivals..tourists.) %>%
  rename(tourist_num = Inbound.arrivals..tourists.) %>%
  mutate(log_tourist = log(tourist_num))

###What is the name of the country that is not matched?
tourist_cty <- unique(as.vector(tourist$Entity))

tourist_added <- data.frame(Entity=c("Fiji, Kiribati, Nauru, Tonga, & Tuvalu", "France & Monaco", "Italy & San Marino", "Madagascar & Comoros", "Mauritius & Seychelles", "New Zealand & Samoa", "Senegal & Guinea-Bissau"),
                            tourist_num = c(398000+4900+37000, 77012000+263000, 39799000+46000, 62000+18900, 682000+132000, 2026000+89000, 427000+43000))

tourist_added <- tourist_added %>%
  mutate(log_tourist = log(tourist_num))

tourist2 <- rbind(tourist, tourist_added)

tourist2 <- tourist2 %>%
  mutate(Entity2 = case_when(Entity=="Brunei" ~ "Brunei Darussalam" ,
                             Entity=="Myanmar" ~ "Burma",
                             Entity=="Cape Verde" ~ "Cabo Verde",
                             Entity=="Congo" ~ "Congo (Republic of)",
                             Entity=="Democratic Republic of Congo" ~ "Congo (DR)",
                             Entity=="Czechia" ~ "Czech Republic",
                             Entity=="Guinea" ~ "Equatorial Guinea",
                             Entity=="Eswatini"~ "Swaziland",
                             #Entity=="Fiji"~ "Fiji, Kiribati, Nauru, Tonga, & Tuvalu",
                             #Entity=="France" ~ "France & Monaco",
                             Entity=="Gambia" ~ "The Gambia",
                             #Entity=="Italy" ~ "Italy & San Marino",
                             Entity=="Kyrgyzstan" ~ "Kyrgyz Republic",
                             #Entity=="Madagascar" ~ "Madagascar & Comoros",
                             #Entity=="Mauritius" ~ "Mauritius & Seychelles",
                             Entity=="Micronesia (country)" ~ "Micronesia",
                             #Entity=="New Zealand" ~ "New Zealand & Samoa",
                             Entity=="North Macedonia" ~ "Macedonia",
                             Entity=="Papua New Guinea" ~ "Papua New Guinea, Solomon Islands & Vanuatu",
                             #Entity=="Senegal" ~ "Senegal & Guinea-Bissau",
                             Entity=="Sudan" ~ "South Sudan",
                             Entity=="Spain" ~ "Spain & Andorra",
                             Entity=="Maldives" ~ "Sri Lanka & Maldives",
                             Entity=="Switzerland" ~ "Switzerland & Liechtenstein",
                             TRUE ~ Entity))

###Combine tourist data to amb_gdp
amb_tourist <- amb_export_ave %>%
  left_join(tourist2, by=c("Country" = "Entity2"))%>%
  select(-Entity)

amb_tourist %>%
  filter(Type_Tertiary3 !="Diplomat") %>%
  ggplot(aes(x = log_tourist, fill=Type_Tertiary3)) +
  geom_histogram(alpha=0.7, position="identity", binwidth=0.2, color = "black") +
  theme_few() +
  theme(legend.position = "bottom") +
  scale_fill_manual(values=c("gray50", "black")) +
  labs(x="Number of Tourists in 2002 in Log", y = "Count of ambassadors",
       fill = "Ambassador Type",
       title = "Distribution of ambassadors (N=327), 2002-2020")

amb_tourist %>%
  ggplot(aes(x = log_tourist, fill=Type_Tertiary3)) +
  geom_histogram(alpha=0.7, position="identity", binwidth=0.2, color = "black") +
  theme_few() +
  theme(legend.position = "bottom") +
  scale_fill_manual(values=c("gray80", "gray50", "black")) +
  labs(x="Number of Tourists in 2002 in Log", y = "Count of ambassadors",
       fill = "Ambassador Type",
       title = "Distribution of ambassadors (N=327), 2002-2020")



# Figure A.3: Export Trend by Year and Country (width=6.5)
top30exp %>%
  ggplot(aes(x = Year, y = total_export_billion, color = country)) +
  geom_point() + 
  geom_line() +
  theme_bw() +
  theme(legend.position="bottom") +
  labs(x="Year", y="Total Exports (Billion)",
       title="US Exports to Top 30 Destinations, 2002-2020") +
  geom_vline(xintercept=2008, linetype=2, color="blue") +
  geom_vline(xintercept=2020, linetype=2, color="blue") +
  annotate("text", x = 2006.5, y = 90, label = "2008 Financial Crisis", color="blue") +
  annotate("text", x = 2018.5, y = 90, label = "COVID-19", color="blue") +
  annotate("text", x = 2017, y = 300, label = "Canada") +
  annotate("text", x = 2017, y = 250, label = "Mexico") +
  annotate("text", x = 2017, y = 130, label = "China")



# Figure A.4 Heteroskedasticity of the Unweighted OLS

mod.main.type.cst.noweight <- felm(log_export ~ ambassador_home_state   | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst)

dt_res <- cbind(export_ambassador_cst[,1:5], mod.main.type.cst.noweight$residuals, mod.main.type.cst.noweight$fitted.values)
colnames(dt_res) <- c("country", "state", "year", "month", "total_export_value", "residual", "fitted.value")

dt_res %>%
  group_by(country, state, year) %>%
  summarize(export_value = sum(total_export_value),
            residual = mean(residual),
            fitted.value = mean(fitted.value)) %>%
  ggplot(aes(x = export_value, y = residual)) +
  geom_point(alpha = 0.5, shape = 1) +
  scale_x_continuous(breaks = c(0, 5e+10, 1e+11),
                     labels = c("$0", "$50 billion", "$100 billion")) +
  labs(x = "Yearly Export Value of Country-State-Year Pairs", y = "Residual") +
  theme_few()



# Table A.5: Home-State Effect by Alternative Weights
mod.main.type.cst.5b   <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 5,], weights = export_ambassador_cst[export_ambassador_cst$rank <= 5,]$wt_csy)
mod.main.type.cst.10b  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 10,], weights = export_ambassador_cst[export_ambassador_cst$rank <= 10,]$wt_csy)
mod.main.type.cst.15b  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 15,], weights = export_ambassador_cst[export_ambassador_cst$rank <= 15,]$wt_csy)
mod.main.type.cst.20b  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 20,], weights = export_ambassador_cst[export_ambassador_cst$rank <= 20,]$wt_csy)
mod.main.type.cst.25b  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 25,], weights = export_ambassador_cst[export_ambassador_cst$rank <= 25,]$wt_csy)
mod.main.type.cst.30b  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 30,], weights = export_ambassador_cst[export_ambassador_cst$rank <= 30,]$wt_csy)

stargazer(mod.main.type.cst.5b, mod.main.type.cst.10b, mod.main.type.cst.15b, 
          mod.main.type.cst.20b, mod.main.type.cst.25b, mod.main.type.cst.30b,
          type = "text",
          column.labels = c("Top 5", "Top 10", "Top 15", "Top20", "Top 25", "Top 30"),
          keep = c("ambassador_home_stateXdiplomat", "ambassador_home_stateXpolitician", "ambassador_home_stateXnon_politician"),
          omit.stat = c("adj.rsq", "f", "ser"),
          add.lines = list(c("Country-State FE", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Country-Time FE", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("State-Time FE", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes")))



# Table A.6: Home-State Effect by Ordinary Least Square (OLS) Regression
mod.main.type.cst.5.ols   <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 5,])
mod.main.type.cst.10.ols  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 10,])
mod.main.type.cst.15.ols  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 15,])
mod.main.type.cst.20.ols  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 20,])
mod.main.type.cst.25.ols  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 25,])
mod.main.type.cst.30.ols  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 30,])

stargazer(mod.main.type.cst.5.ols, mod.main.type.cst.10.ols, mod.main.type.cst.15.ols, 
          mod.main.type.cst.20.ols, mod.main.type.cst.25.ols, mod.main.type.cst.30.ols,
          type = "text",
          column.labels = c("Top 5", "Top 10", "Top 15", "Top20", "Top 25", "Top 30"),
          keep = c("ambassador_home_stateXdiplomat", "ambassador_home_stateXpolitician", "ambassador_home_stateXnon_politician"),
          omit.stat = c("adj.rsq", "f", "ser"),
          add.lines = list(c("Country-State FE", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("Country-Time FE", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes"),
                           c("State-Time FE", "Yes", "Yes", "Yes", "Yes", "Yes", "Yes")))


# Table A.7: Robustness Checks Controlling Selection Effects
mod.main.cst.robust  <- felm(log_export ~ ambassador_home_state + swingstate_non_vacant + corestate_non_vacant | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador_cst,  weights = export_ambassador_cst$wt_cs)
mod.main.type.cst.robust <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician  + swingstate_non_vacant + corestate_non_vacant | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst, weights = export_ambassador_cst$wt_cs)

stargazer(mod.main.cst.robust, mod.main.type.cst.robust,
          type = "text",
          omit.stat = c("adj.rsq", "f", "ser"),
          add.lines = list(c("Country-State FE", "Yes", "Yes"),
                           c("Country-Time FE", "Yes", "Yes"),
                           c("State-Time FE", "Yes", "Yes")))



# Figure A.5: A Placebo Case of a Politician Ambassador Who Declined O↵er

### David Wilkins ###

placebo <- export_ambassador %>%
  filter(country == "Chile" & year %in% 2001:2006) %>%
  mutate(home_state2 = ifelse(state == "South Carolina", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  mutate(month = as.numeric(month)) %>%
  mutate(time = case_when(year <= 2002 ~ 1,
                          year == 2003 & month < 3 ~ 1,
                          year == 2003 & month >= 3 ~ 2,
                          year == 2004 & month < 7 ~ 2,
                          year == 2004 & month >= 7 ~ 3,
                          year >= 2005 ~ 3)) %>%
  group_by(time, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2002.3,
         time_end = 2004.7) %>%
  mutate(country_name = "Wilkins (SC - Canada)") %>%
  mutate(position = 1) 

placebo <- export_ambassador %>%
  filter(country == "Chile" & year %in% 2001:2005) %>%
  mutate(home_state2 = ifelse(state == "South Carolina", " Placebo Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  mutate(month = as.numeric(month)) %>%
  group_by(year, month, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(month2 = (month - 1) / 12) %>%
  mutate(time = year + month2) %>%
  mutate(time_start = 2002 + (3-1)/12,
         time_end = 2004 + (7-1)/12) 

placebo %>%
  filter(time < 2004.7) %>%
  ggplot(aes(x = time, y = log_export, color = home_state2)) +
  #  geom_line(size = 1.1, alpha = 0.8) +
  geom_point(size = 2.5, alpha = 0.5) +
  geom_smooth(se = F, method = lm) +
  geom_vline(aes(xintercept = time_start), linetype = 2) +
  geom_vline(aes(xintercept = time_end), linetype = 2) +
  scale_color_manual(values = c("blue2", "grey40")) +
  scale_x_continuous(breaks = c(2002, 2003, 2004, 2005),
                     labels = c("2002-01", "2003-01", "2004-01", "2005-01")) +
  labs(x = "Time (Year-Month)", y = "Average Export in Log", color = "",
       title = "Export from South Carolina to Chile") +
  theme_few() +
  theme(legend.position = "bottom")



# Table A.8: A Placebo Analysis of a Politician Ambassador Who Declined O↵er (Only Export to Chile)
export_ambassador_cst_chile <- export_ambassador_cst %>%
  filter(country == "Chile") %>%
  mutate(ambassador_home_stateXpolitician_placebo = ifelse(lastname == "Brownfield" & state == "South Carolina", 1, 0))

table(export_ambassador_cst_chile$lastname, export_ambassador_cst_chile$type3)

mod.main.chile.placebo  <- felm(log_export ~ ambassador_home_stateXpolitician_placebo + ambassador_home_stateXdiplomat | state + monthyear | 0 | state + monthyear, data = export_ambassador_cst_chile,  weights = export_ambassador_cst_chile$wt_cs)

stargazer(mod.main.chile.placebo,
          type = "text",
          omit.stat = c("adj.rsq", "f", "ser"),
          covariate.labels = c("Placebo Politician's Home State",
                               "Diplomat's Home State"),
          add.lines = list(c("State FE", "Yes"),
                           c("Time FE", "Yes")))



# Table A.9: Home-State Effect with Diplomats Excluded
mod.main.cst.d.ex  <- felm(log_export ~ ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$diplomat != 1,],  weights = export_ambassador_cst[export_ambassador_cst$diplomat != 1,]$wt_cs)

stargazer(mod.main.type.cst, mod.main.cst.d.ex, 
          type = "text",
          omit.stat = c("adj.rsq", "f", "ser"),
          add.lines = list(c("Country-State FE", "Yes", "Yes","Yes"),
                           c("Country-Time FE", "Yes", "Yes", "Yes"),
                           c("State-Time FE", "Yes", "Yes", "Yes")))



# Table A.10: Home-State Effect with DC, VA, MD Excluded
## Do we see a home-state effect for those who only have work experience in the federal government? 

mod.main.type.cst.exc <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$state != "Virginia" & export_ambassador_cst$state != "Maryland" & export_ambassador_cst$state != "Dist of Columbia", ], weights = export_ambassador_cst[export_ambassador_cst$state != "Virginia" & export_ambassador_cst$state != "Maryland" & export_ambassador_cst$state != "Dist of Columbia", ]$wt_cs)

stargazer(mod.main.type.cst, mod.main.type.cst.exc, 
          type = "text",
          omit.stat = c("adj.rsq", "f", "ser"),
          column.labels = c("All", "Excl. DC, VA, MD"),
          add.lines = list(c("Country-State FE", "Yes", "Yes"),
                           c("Country-Time FE", "Yes", "Yes"),
                           c("State-Time FE", "Yes", "Yes")))

export_state <- export_all %>%
  group_by(state) %>%
  summarize(total_export_value = sum(total_export_value))

sum(export_state[export_state$state %in% c("Dist of Columbia", "Virginia", "Maryland"),]$total_export_value) / sum(export_state$total_export_value)



# Table A.11: Welfare Analysis of Politician Ambassadors on US Exports
# Do political ambassadors increase the size of the pie OR give a larger slice of the pie to their home state?

mod.main.type.welfare.ct0 <- felm(log_export ~ diplomat + politician + non_politician | country + monthyear  | 0 | country + monthyear, data = export_ambassador_ct)
mod.main.type.welfare.ct <- felm(log_export ~ diplomat + politician + non_politician + destin_gdp_logged + destin_pop_logged  | country + monthyear  | 0 | country + monthyear, data = export_ambassador_ct)

mod.main.type.welfare.ct.wt1 <- felm(log_export ~ diplomat + politician + non_politician + destin_gdp_logged + destin_pop_logged | country + monthyear  | 0 | country+ monthyear, data = export_ambassador_ct, weights = export_ambassador_ct$wt_c)

stargazer(mod.main.type.welfare.ct0, mod.main.type.welfare.ct,
          type = "text",
          omit.stat = c("adj.rsq", "f", "ser"),
          add.lines = list(c("Country FE", "Yes", "Yes"),
                           c("Time FE", "Yes", "Yes")))



# Table A.12: Home-State Effect with Alternative Measures of Politician Ambassadors
mod.main.type.cst.expanded1 <- felm(log_export ~ ambassador_home_stateXdiplomat1 + ambassador_home_stateXpolitician1 + ambassador_home_stateXnon_politician1 | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst, weights = export_ambassador_cst$wt_cs)
mod.main.type.cst.expanded2 <- felm(log_export ~ ambassador_home_stateXdiplomat2 + ambassador_home_stateXpolitician2 + ambassador_home_stateXnon_politician2 | countryXstate + countryXmonthyear + stateXmonthyear  | 0 | countryXstate + monthyear, data = export_ambassador_cst, weights = export_ambassador_cst$wt_cs)

stargazer(mod.main.type.cst, mod.main.type.cst.expanded1, mod.main.type.cst.expanded2,
          type = "text",
          omit.stat = c("adj.rsq", "f", "ser"),
          add.lines = list(c("Country-State FE", "Yes", "Yes", "Yes"),
                           c("Country-Time FE", "Yes", "Yes", "Yes"),
                           c("State-Time FE", "Yes", "Yes", "Yes")))



# Table A.13: Home-State Effect by Industry
mod_naics_1  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[1 ],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[1 ],]$wt_csi)
mod_naics_2  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[2 ],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[2 ],]$wt_csi)
mod_naics_3  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[3 ],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[3 ],]$wt_csi)
mod_naics_4  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[4 ],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[4 ],]$wt_csi)
mod_naics_5  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[5 ],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[5 ],]$wt_csi)
mod_naics_6  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[6 ],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[6 ],]$wt_csi)
mod_naics_7  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[7 ],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[7 ],]$wt_csi)
mod_naics_8  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[8 ],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[8 ],]$wt_csi)
mod_naics_9  <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[9 ],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[9 ],]$wt_csi)
mod_naics_10 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[10],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[10],]$wt_csi)
mod_naics_11 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[11],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[11],]$wt_csi)
mod_naics_12 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[12],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[12],]$wt_csi)
mod_naics_13 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[13],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[13],]$wt_csi)
mod_naics_14 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[14],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[14],]$wt_csi)
mod_naics_15 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[15],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[15],]$wt_csi)
mod_naics_16 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[16],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[16],]$wt_csi)
mod_naics_17 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[17],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[17],]$wt_csi)
mod_naics_18 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[18],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[18],]$wt_csi)
mod_naics_19 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[19],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[19],]$wt_csi)
mod_naics_20 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[20],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[20],]$wt_csi)
mod_naics_21 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[21],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[21],]$wt_csi)
mod_naics_22 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[22],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[22],]$wt_csi)
mod_naics_23 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[23],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[23],]$wt_csi)
mod_naics_24 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[24],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[24],]$wt_csi)
mod_naics_25 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[25],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[25],]$wt_csi)
mod_naics_26 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[26],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[26],]$wt_csi)
mod_naics_27 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[27],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[27],]$wt_csi)
mod_naics_28 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[28],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[28],]$wt_csi)
mod_naics_29 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[29],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[29],]$wt_csi)
mod_naics_30 <- felm(log_export ~ ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[30],], weight = export_ambassador[export_ambassador$rank <= 10 & export_ambassador$naics == naics[30],]$wt_csi)


stargazer(mod_naics_1, mod_naics_2, mod_naics_3, mod_naics_4, mod_naics_5, 
          type = "text",
          column.labels = c("Transportation", "Computer", "Chemicals", "Machinery", "Manufactured"),
          covariate.labels = c("Diplomat's Home State", "Politician's Home State", "Non-politician's Home State"),
          omit.stat = c("adj.rsq", "f", "ser"))

stargazer(mod_naics_6, mod_naics_7, mod_naics_8, mod_naics_9, mod_naics_10, 
          type = "text",
          column.labels = c("Petroleum", "Metal", "Agricultural", "Food", "Electrical"),
          covariate.labels = c("Diplomat's Home State", "Politician's Home State", "Non-politician's Home State"),
          omit.stat = c("adj.rsq", "f", "ser"))

stargazer(mod_naics_11, mod_naics_12, mod_naics_13, mod_naics_14, mod_naics_15, 
          type = "text",
          column.labels = c("Metal", "Plastics", "Oil&Gas", "Paper", "Waste"),
          covariate.labels = c("Diplomat's Home State", "Politician's Home State", "Non-politician's Home State"),
          omit.stat = c("adj.rsq", "f", "ser"))

stargazer(mod_naics_16, mod_naics_17, mod_naics_18, mod_naics_19, mod_naics_20, 
          type = "text",
          column.labels = c("Minerals", "Mineral", "Textiles", "Beverages&Tobacco", "Printed"),
          covariate.labels = c("Diplomat's Home State", "Politician's Home State", "Non-politician's Home State"),
          omit.stat = c("adj.rsq", "f", "ser"))

stargazer(mod_naics_21, mod_naics_22, mod_naics_23, mod_naics_24, mod_naics_25, 
          type = "text",
          column.labels = c("Wood", "Fish", "Apparel", "Furniture", "Leather"),
          covariate.labels = c("Diplomat's Home State", "Politician's Home State", "Non-politician's Home State"),
          omit.stat = c("adj.rsq", "f", "ser"))

stargazer(mod_naics_26, mod_naics_27, mod_naics_28,  mod_naics_29, mod_naics_30, 
          type = "text",
          column.labels = c("Mill", "Forestry", "Livestock", "Books", "Music"),
          covariate.labels = c("Diplomat's Home State", "Politician's Home State", "Non-politician's Home State"),
          omit.stat = c("adj.rsq", "f", "ser"))

rm(mod_naics_1, mod_naics_2, mod_naics_3, mod_naics_4, mod_naics_5, mod_naics_6, mod_naics_7, mod_naics_8, mod_naics_9, mod_naics_10, mod_naics_11, mod_naics_12, mod_naics_13, mod_naics_14, mod_naics_15, mod_naics_16, mod_naics_17, mod_naics_18, mod_naics_19, mod_naics_20, mod_naics_21, mod_naics_22, mod_naics_23, mod_naics_24, mod_naics_25, mod_naics_26, mod_naics_27, mod_naics_28,  mod_naics_29, mod_naics_30)


# Table A.14: Industry Upstreamness of US Production
upstreamness <- read_csv("3digit_upstreamness_by_industry_ACFH2012.csv")

upstreamness %>%
  na.omit(naics) %>%
  group_by(naics) %>%
  summarize(upstreamness = mean(upstreamness, na.rm = T)) %>%
  arrange(desc(upstreamness))



# Event Study
# Figure A.6: Pre- and Post-Trend of the Home State Effect, Politician Ambassadors Only

# pre and post for all 
event1 <- export_ambassador %>%
  filter(country == "Canada" & year %in% 2004:2010) %>%
  mutate(home_state2 = ifelse(state == "South Carolina", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2005.5,
         time_end = 2009) %>%
  mutate(country_name = "Wilkins (SC - Canada)") %>%
  mutate(position = 1) %>%
  mutate(position2 = 1)

event2 <- export_ambassador %>%
  filter(country == "Mexico" & year %in% 2002:2011) %>%
  mutate(home_state2 = ifelse(state == "Texas", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2002.92,
         time_end = 2009) %>%
  mutate(country_name = "Garza (TX - Mexico)") %>%
  mutate(position = 2) %>%
  mutate(position2 = 2)

event3 <- export_ambassador %>%
  filter(country == "China" & year %in% 2008:2013) %>%
  mutate(home_state2 = ifelse(state == "Utah", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2009.75,
         time_end = 2011.25) %>%
  mutate(country_name = "Huntsman (UT - China)") %>%
  mutate(position = 3)  %>%
  mutate(position2 = 3)

event4 <- export_ambassador %>%
  filter(country == "China" & year %in% 2010:2016) %>%
  mutate(home_state2 = ifelse(state == "Washington", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2011.58,
         time_end = 2014.) %>%
  mutate(country_name = "Locke (WA - China)") %>%
  mutate(position = 4.08)  %>%
  mutate(position2 = 4)

event5 <- export_ambassador %>%
  filter(country == "China" & year %in% 2013:2018) %>%
  mutate(home_state2 = ifelse(state == "Montana", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2014.25,
         time_end = 2017) %>%
  mutate(country_name = "Baucus (MT - China)") %>%
  mutate(position = 5) %>%
  mutate(position2 = 5)

event6 <- export_ambassador %>%
  filter(country == "China" & year %in% 2016:2022) %>%
  mutate(home_state2 = ifelse(state == "Iowa", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2017.5,
         time_end = 2020.67) %>%
  mutate(country_name = "Branstad (IA - China)") %>%
  mutate(position = 6) %>%
  mutate(position2 = NA)

event7 <- export_ambassador %>%
  filter(country == "Japan" & year %in% 2004:2010) %>%
  mutate(home_state2 = ifelse(state == "Texas", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2005.83,
         time_end = 2009) %>%
  mutate(country_name = "Schieffer (TX - Japan)") %>%
  mutate(position = 7)  %>%
  mutate(position2 = 6)

event8 <- export_ambassador %>%
  filter(country == "Japan" & year %in% 2016:2021) %>%
  mutate(home_state2 = ifelse(state == "Tennessee", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2017.58,
         time_end = 2019.5) %>%
  mutate(country_name = "Hagerty (TN - Japan)") %>%
  mutate(position = 8)  %>%
  mutate(position2 = NA)

event9 <- export_ambassador %>%
  filter(country == "Germany" & year %in% 2008:2015) %>%
  mutate(home_state2 = ifelse(state == "New Jersey", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2009.67,
         time_end = 2013.58) %>%
  mutate(country_name = "Murphy (NJ - Germany)") %>%
  mutate(position = 9)  %>%
  mutate(position2 = 7)

event10 <- export_ambassador %>%
  filter(country == "Netherlands" & year %in% 2016:2023) %>%
  mutate(home_state2 = ifelse(state == "Michigan", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2018,
         time_end = 2021) %>%
  mutate(country_name = "Hoekstra (MI - Netherlands)") %>%
  mutate(position = 10) %>%
  mutate(position2 = NA)

event11 <- export_ambassador %>%
  filter(country == "India" & year %in% 2008:2013) %>%
  mutate(home_state2 = ifelse(state == "Indiana", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2009.58,
         time_end = 2011.5) %>%
  mutate(country_name = "Roemer (IN - India)") %>%
  mutate(position = 11)  %>%
  mutate(position2 = 8)

event12 <- export_ambassador %>%
  filter(country == "Belgium" & year %in% 2017:2023) %>%
  mutate(home_state2 = ifelse(state == "Illinois", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2018.5,
         time_end = 2021) %>%
  mutate(country_name = "Gidwitz (IL - Belgium)") %>%
  mutate(position = 12) %>%
  mutate(position2 = NA)

event13 <- export_ambassador %>%
  filter(country == "Singapore" & year %in% 2004:2010) %>%
  mutate(home_state2 = ifelse(state == "Washington", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2005.92,
         time_end = 2009) %>%
  mutate(country_name = "Herbold (WA - Singapore)") %>%
  mutate(position = 13) %>%
  mutate(position2 = 9)

event14 <- export_ambassador %>%
  filter(country == "Australia" & year %in% 2012:2018) %>%
  mutate(home_state2 = ifelse(state == "Maryland", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2013.67,
         time_end = 2016.67) %>%
  mutate(country_name = "Berry (MD - Australia)") %>%
  mutate(position = 14) %>%
  mutate(position2 = 10)

event15 <- export_ambassador %>%
  filter(country == "Switzerland" & year %in% 2005:2010) %>%
  mutate(home_state2 = ifelse(state == "Texas", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2006.75,
         time_end = 2008.92) %>%
  mutate(country_name = "Coneway (TX - Switzerland)") %>%
  mutate(position = 15) %>%
  mutate(position2 = 11)

event16 <- export_ambassador %>%
  filter(country == "Switzerland" & year %in% 2008:2015) %>%
  mutate(home_state2 = ifelse(state == "Virginia", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2009.58,
         time_end = 2013.33) %>%
  mutate(country_name = "Beyer (VA - Switzerland)") %>%
  mutate(position = 16) %>%
  mutate(position2 = 12)

event17 <- export_ambassador %>%
  filter(country == "Spain" & year %in% 2004:2010) %>%
  mutate(home_state2 = ifelse(state == "Texas", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2005.42,
         time_end = 2009) %>%
  mutate(country_name = "Aguirre (TX - Spain)") %>%
  mutate(position = 17) %>%
  mutate(position2 = 13)

event18 <- export_ambassador %>%
  filter(country == "Argentina" & year %in% 2013:2019) %>%
  mutate(home_state2 = ifelse(state == "California", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2015,
         time_end = 2017) %>%
  mutate(country_name = "Mamet (CA - Argentina)") %>%
  mutate(position = 18) %>%
  mutate(position2 = 14)

event19 <- export_ambassador %>%
  filter(country == "Dominican Republic" & year %in% 2006:2011) %>%
  mutate(home_state2 = ifelse(state == "Arizona", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2007.92,
         time_end = 2009) %>%
  mutate(country_name = "Fannin (AZ - Dominican Rep.)") %>%
  mutate(position = 19) %>%
  mutate(position2 = 15)

event20 <- export_ambassador %>%
  filter(country == "Italy" & year %in% 2016:2020) %>%
  mutate(home_state2 = ifelse(state == "New Jersey", "Home State", "Others")) %>%
  mutate(year = as.numeric(year)) %>%
  group_by(year, home_state2) %>%
  summarize(total_export_value = mean(total_export_value)) %>%
  mutate(log_export = log(total_export_value)) %>%
  mutate(time_start = 2016,
         time_end = 2009) %>%
  mutate(country_name = "Fannin (AZ - Dominican Rep.)") %>%
  mutate(position = 19) %>%
  mutate(position2 = 15)

event <- rbind(event1, event2, event3, event4, event5, event6, event7, event8, event9, event10,
               event11, event12, event13, event14, event15, event16, event17, event18, event19)

rm(event1, event2, event3, event4, event5, event6, event7, event8, event9, event10,
   event11, event12, event13, event14, event15, event16, event17, event18, event19)

event %>%
  filter(!is.na(position2)) %>%
  ggplot(aes(x = year, y = log_export, color = home_state2)) +
  geom_vline(aes(xintercept = time_start), linetype = 2) +
  geom_vline(aes(xintercept = time_end), linetype = 2) +
  geom_line(size = 1.1, alpha = 0.8) +
  geom_point(size = 2.5, alpha = 0.8) +
  facet_wrap(~ reorder(country_name, position2), scales = "free", ncol = 4) +
  scale_color_manual(values = c("blue2", "grey40")) +
  labs(x = "Year", y = "Average Export in Log", color = "") +
  theme_few() +
  theme(legend.position = c(0.85, 0.1))


# Figure A.7: Age of Politician Ambassadors to Run for an Election
amb30 <- read.csv("ambassador_us_30.csv")
polamb <- amb30 %>% filter(type == "politician")
polamb_reran <- polamb %>% filter(!is.na(reran_election))
polamb <- polamb %>% mutate(reran_rev = ifelse(is.na(reran_election), "No", "Yes"))
polamb$reran_rev <- as.factor(polamb$reran_rev)

df <- polamb%>%group_by(reran_rev)%>%
  summarise(avage = mean(age))


polamb %>%
  ggplot(aes(x = reran_rev, y = age)) + 
  geom_boxplot(fill="lightblue", colour="black") + 
  geom_jitter(shape = 16, position = position_jitter(0.2), colour = "blue") +
  labs(x="Ran for an election after serving as an ambassador?", y="Age", 
       title="Relationship between age and running for an election \n N=23, Politician ambassadors") +
  theme_bw()




# Figure A.8: Marginal Plots of Home-State Effect on Experience and Age
mod.main.exp.p.cst  <- felm(log_export ~ ambassador_home_state * politician * experience | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 10,],  weights = export_ambassador_cst[export_ambassador_cst$rank <= 10,]$wt_cs)
mod.main.age.p.cst  <- felm(log_export ~ ambassador_home_state * politician * age | countryXstate + countryXmonthyear + stateXmonthyear | 0 | countryXstate + monthyear, data = export_ambassador_cst[export_ambassador_cst$rank <= 10,],  weights = export_ambassador_cst[export_ambassador_cst$rank <= 10,]$wt_cs)

### Experience ###

## politician
#beta_1
beta_1 = mod.main.exp.p.cst$coefficients["ambassador_home_state",]
#beta_2
beta_2 = mod.main.exp.p.cst$coefficients["ambassador_home_state:politician",]
#beta_3
beta_3 = mod.main.exp.p.cst$coefficients["ambassador_home_state:politician:experience",]


# extract Variance Covariance matrix
covMat = vcov(mod.main.exp.p.cst)

#Set range of the moderator variable
min_val = 1
max_val = 42
num_points = 50

increment = (max_val - min_val) / (num_points - 1)

# Create list of moderator values at which marginal effect is evaluated
a <- seq(from = min_val, to = max_val, by = increment)


# Compute marginal effects
delta = beta_1 + beta_2 + a * beta_3


# Compute variances
var_1  = covMat["ambassador_home_state", "ambassador_home_state"]
var_2  = covMat["ambassador_home_state:politician", "ambassador_home_state:politician"]
var_3  = covMat["ambassador_home_state:politician:experience", "ambassador_home_state:politician:experience"]
cov_12 = covMat["ambassador_home_state", "ambassador_home_state:politician"]
cov_13 = covMat["ambassador_home_state", "ambassador_home_state:politician:experience"]
cov_23 = covMat["ambassador_home_state:politician", "ambassador_home_state:politician:experience"]

var_delta = var_1 + var_2 + a^2 * var_3 + 2 * cov_12 + 2 * a * cov_13 + 2 * a * cov_23

# Standard errors
se_delta = sqrt(var_delta)

# Upper and lower confidence bounds
z_score = qnorm(1 - ((1 - 0.9)/2))
upper_bound = delta + z_score * se_delta
lower_bound = delta - z_score * se_delta

# Determine the bounds of the graphing area
max_y = max(upper_bound)
min_y = min(lower_bound)


### Draw the graph

# Make the histogram color
makeTransparent<-function(someColor, alpha=80){
  newColor<-col2rgb(someColor)
  apply(newColor, 2, function(curcoldata){rgb(red=curcoldata[1], green=curcoldata[2],
                                              blue=curcoldata[3],alpha=alpha, maxColorValue=255)})
}
hist_col = makeTransparent("grey")

# Initialize plotting window
plot(x=c(), y=c(), ylim=c(min_y, max_y), xlim=c(min_val, max_val), xlab= "Experience", ylab= "Marginal Home-State Effect on Experience", main="Marginal Home-State Effect of Politician Ambassadors on Experience")

# Add a dashed horizontal line for zero
abline(h=0, lty=3, col="red")

# Plot estimated effects
lines(y=delta, x=a, lwd=2)
lines(y=upper_bound, x=a, lty=2)
lines(y=lower_bound, x=a, lty=2)


# Add a vertical line at the median
#abline(v = median(ambassador_raw[["experience"]], na.rm = T), lty=3, col="blue")

# Add Rug plot
rug(ambassador_raw[["experience"]], lwd = 1.5)
# Add Histogram (Histogram only plots when minimum and maximum are the min/max of the moderator)
par(new=T, c(min_y, 0))
mod_frame = model.frame(mod.main.exp.p.cst)
hist(mod_frame[,"experience"], axes=F, xlab="", ylab="",main="", border=hist_col, col=hist_col)

rm(mod_frame)



### Age ###

## politician
#beta_1
beta_1 = mod.main.age.p.cst$coefficients["ambassador_home_state",]
#beta_2
beta_2 = mod.main.age.p.cst$coefficients["ambassador_home_state:politician",]
#beta_3
beta_3 = mod.main.age.p.cst$coefficients["ambassador_home_state:politician:age",]


# extract Variance Covariance matrix
covMat = vcov(mod.main.age.p.cst)

#Set range of the moderator variable
min_val = 41
max_val = 76
num_points = 50

increment = (max_val - min_val) / (num_points - 1)

# Create list of moderator values at which marginal effect is evaluated
a <- seq(from = min_val, to = max_val, by = increment)


# Compute marginal effects
delta = beta_1 + beta_2 + a * beta_3


# Compute variances
var_1  = covMat["ambassador_home_state", "ambassador_home_state"]
var_2  = covMat["ambassador_home_state:politician", "ambassador_home_state:politician"]
var_3  = covMat["ambassador_home_state:politician:age", "ambassador_home_state:politician:age"]
cov_12 = covMat["ambassador_home_state", "ambassador_home_state:politician"]
cov_13 = covMat["ambassador_home_state", "ambassador_home_state:politician:age"]
cov_23 = covMat["ambassador_home_state:politician", "ambassador_home_state:politician:age"]

var_delta = var_1 + var_2 + a^2 * var_3 + 2 * cov_12 + 2 * a * cov_13 + 2 * a * cov_23


# Standard errors
se_delta = sqrt(var_delta)

# Upper and lower confidence bounds
z_score = qnorm(1 - ((1 - 0.9)/2))
upper_bound = delta + z_score * se_delta
lower_bound = delta - z_score * se_delta

# Determine the bounds of the graphing area
max_y = max(upper_bound)
min_y = min(lower_bound)


### Draw the graph

# Make the histogram color
makeTransparent<-function(someColor, alpha=80){
  newColor<-col2rgb(someColor)
  apply(newColor, 2, function(curcoldata){rgb(red=curcoldata[1], green=curcoldata[2],
                                              blue=curcoldata[3],alpha=alpha, maxColorValue=255)})
}
hist_col = makeTransparent("grey")

# Initialize plotting window
plot(x=c(), y=c(), ylim=c(min_y, max_y), xlim=c(min_val, max_val), xlab= "Age", ylab= "Marginal Home-State Effect on Age", main="Marginal Home-State Effect of Politician Ambassadors on Age")

# Add a dashed horizontal line for zero
abline(h=0, lty=3, col="red")

# Plot estimated effects
lines(y=delta, x=a, lwd=2)
lines(y=upper_bound, x=a, lty=2)
lines(y=lower_bound, x=a, lty=2)


# Add a vertical line at the median
abline(v = median(ambassador_raw[["age"]]), lty=3, col="blue")

# Add Rug plot
rug(ambassador_raw[["age"]], lwd = 1.5)
# Add Histogram (Histogram only plots when minimum and maximum are the min/max of the moderator)
par(new=T, c(min_y, 0))
mod_frame = model.frame(mod.main.age.p.cst)
hist(mod_frame[,"age"], axes=F, xlab="", ylab="",main="", border=hist_col, col=hist_col)

rm(mod_frame)




# Table A.15: Home-State Effect of Politician Ambassadors to Japan
export_ambassador_cst_japan <- export_ambassador_cst %>%
  filter(country == "Japan") %>%
  mutate(ambassador_home_stateXBaker = ifelse(lastname == "Baker", ambassador_home_state, 0),
         ambassador_home_stateXSchieffer = ifelse(lastname == "Schieffer", ambassador_home_state, 0),
         ambassador_home_stateXHagerty = ifelse(lastname == "Hagerty", ambassador_home_state, 0))

mod.main.japan  <- felm(log_export ~ ambassador_home_stateXHagerty + ambassador_home_stateXSchieffer + ambassador_home_stateXBaker | state + monthyear | 0 | state + monthyear, data = export_ambassador_cst_japan,  weights = export_ambassador_cst_japan$wt_cs)

stargazer(mod.main.japan,
          type = "text",
          omit.stat = c("adj.rsq", "f", "ser"),
          covariate.labels = c("Hagerty's Home State",
                               "Schieffer's Home State",
                               "Baker's Home State"),
          add.lines = list(c("State FE", "Yes"),
                           c("Time FE", "Yes")))

# Table A.16: Home-State Effect and Ambassador Types (Relaxed)
mod.main.type.cst.relax  <- felm(log_export ~ diplomat + politician + non_politician + ambassador_home_stateXdiplomat + ambassador_home_stateXpolitician + ambassador_home_stateXnon_politician + destin_gdp_logged + destin_pop_logged + age + as.factor(female) + black + hispanic + asian | countryXstate + stateXmonthyear | 0 | country+ monthyear, data = export_ambassador_cst,  weights = export_ambassador_cst$wt_cs)

stargazer(mod.main.type.cst.relax,
          type = "text",
          omit.stat = c("adj.rsq", "f", "ser"),
          add.lines = list(c("Country-State FE", "Yes"),
                           c("Country-Time FE", ""),
                           c("State-Time FE", "Yes")))

